{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "73a34898",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'refresh_token': '25.9433d7e9bbd35d1febbb2bb376b44732.315360000.1972820897.282335-26670667', 'expires_in': 2592000, 'session_key': '9mzdAvbh2DbtwRaMxo3VKzTi4ozWBAxrrJcLiI3hbTcB0HmM4a0jUwEVFBnkKFwIdesFOvOji3PmgWM0ZZUA0gMfM4nSew==', 'access_token': '24.947ef2126cc33513988b7a2e1077b259.2592000.1660052897.282335-26670667', 'scope': 'public vis-classify_dishes vis-classify_car brain_all_scope vis-classify_animal vis-classify_plant brain_object_detect brain_realtime_logo brain_dish_detect brain_car_detect brain_animal_classify brain_plant_classify brain_ingredient brain_advanced_general_classify brain_custom_dish brain_poi_recognize brain_vehicle_detect brain_redwine brain_currency brain_vehicle_damage brain_multi_ object_detect wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online smartapp_gov_aladin_to_xcx', 'session_secret': '3fbaae015eccf2453826f6990f0dca9c'}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "import requests \n",
    "\n",
    "# client_id 为官网获取的AK， client_secret 为官网获取的SK\n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?'\n",
    "payload = {\n",
    "    'grant_type':'client_credentials',\n",
    "    'client_id':'yWwo70MyVuxGNboC0oC6tRGf',\n",
    "    'client_secret':'eBPtyfCGMDQIg1VcG6tW8IW7BebkcNf8'\n",
    "}\n",
    "response = requests.get(host,params=payload)\n",
    "if response:\n",
    "    print(response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "22eeb4fc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'24.947ef2126cc33513988b7a2e1077b259.2592000.1660052897.282335-26670667'"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "access_token = response.json()['access_token']\n",
    "access_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "cce49144",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'refresh_token': '25.9433d7e9bbd35d1febbb2bb376b44732.315360000.1972820897.282335-26670667',\n",
       " 'expires_in': 2592000,\n",
       " 'session_key': '9mzdAvbh2DbtwRaMxo3VKzTi4ozWBAxrrJcLiI3hbTcB0HmM4a0jUwEVFBnkKFwIdesFOvOji3PmgWM0ZZUA0gMfM4nSew==',\n",
       " 'access_token': '24.947ef2126cc33513988b7a2e1077b259.2592000.1660052897.282335-26670667',\n",
       " 'scope': 'public vis-classify_dishes vis-classify_car brain_all_scope vis-classify_animal vis-classify_plant brain_object_detect brain_realtime_logo brain_dish_detect brain_car_detect brain_animal_classify brain_plant_classify brain_ingredient brain_advanced_general_classify brain_custom_dish brain_poi_recognize brain_vehicle_detect brain_redwine brain_currency brain_vehicle_damage brain_multi_ object_detect wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online smartapp_gov_aladin_to_xcx',\n",
       " 'session_secret': '3fbaae015eccf2453826f6990f0dca9c'}"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "5be9c3d5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'24.947ef2126cc33513988b7a2e1077b259.2592000.1660052897.282335-26670667'"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xiaoyang_API = response.json()['access_token']\n",
    "xiaoyang_API "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ca1c0e59",
   "metadata": {},
   "source": [
    "# 植物识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "1fa0dfce",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'result': [{'score': 0.8872193, 'name': '郁金香'}, {'score': 0.085323244, 'name': '马蹄莲'}, {'score': 0.01163359, 'name': '洋桔梗'}], 'log_id': 1546129403726783319}\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "植物识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v1/plant\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('images/hua.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = xiaoyang_API\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "b87a0901",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'result': [{'score': 0.8872193, 'name': '郁金香'},\n",
       "  {'score': 0.085323244, 'name': '马蹄莲'},\n",
       "  {'score': 0.01163359, 'name': '洋桔梗'}],\n",
       " 'log_id': 1546129403726783319}"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "response.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d50886e1",
   "metadata": {},
   "source": [
    "# 商品图片搜索"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "29cc8aba",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'refresh_token': '25.9d4f6e6d4ede79489a644e4461d0ac12.315360000.1972823546.282335-26670737', 'expires_in': 2592000, 'session_key': '9mzdDtR7Fs5fhsB4VGhgi7GZn0S9uNGZnvEqyQNvMOSlOi7I686/dA7UF7WAEvyLOVjRfljJYbtGxMoHm9OxML3NNqchVw==', 'access_token': '24.5f5e69d2612908dc31efb69e290fb333.2592000.1660055546.282335-26670737', 'scope': 'public brain_all_scope vis-classify_实时检索-相似 brain_realtime_same_hq brain_realtime_similar brain_realtime_product brain_realtime_picturebook wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online smartapp_gov_aladin_to_xcx', 'session_secret': 'c4b5a80cc05f225a28fc45a488250d01'}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "import requests \n",
    "\n",
    "# client_id 为官网获取的AK， client_secret 为官网获取的SK\n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?'\n",
    "payload = {\n",
    "    'grant_type':'client_credentials',\n",
    "    'client_id':'fPx21RVyGCNO9a8Cx8NNewZ0',\n",
    "    'client_secret':'OjDubYnPXHSjTqrMnHX3iPFmR9vQgn1U'\n",
    "}\n",
    "response = requests.get(host,params=payload)\n",
    "if response:\n",
    "    print(response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "569c12cf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'24.5f5e69d2612908dc31efb69e290fb333.2592000.1660055546.282335-26670737'"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "access_token = response.json()['access_token']\n",
    "access_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "99035154",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'refresh_token': '25.9d4f6e6d4ede79489a644e4461d0ac12.315360000.1972823546.282335-26670737',\n",
       " 'expires_in': 2592000,\n",
       " 'session_key': '9mzdDtR7Fs5fhsB4VGhgi7GZn0S9uNGZnvEqyQNvMOSlOi7I686/dA7UF7WAEvyLOVjRfljJYbtGxMoHm9OxML3NNqchVw==',\n",
       " 'access_token': '24.5f5e69d2612908dc31efb69e290fb333.2592000.1660055546.282335-26670737',\n",
       " 'scope': 'public brain_all_scope vis-classify_实时检索-相似 brain_realtime_same_hq brain_realtime_similar brain_realtime_product brain_realtime_picturebook wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online smartapp_gov_aladin_to_xcx',\n",
       " 'session_secret': 'c4b5a80cc05f225a28fc45a488250d01'}"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "4c9d6f08",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'24.5f5e69d2612908dc31efb69e290fb333.2592000.1660055546.282335-26670737'"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xiaoyang1_API = response.json()['access_token']\n",
    "xiaoyang1_API "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "4c204530",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'result': [{'score': 0.99732199081379, 'brief': '1', 'cont_sign': '2608927001,1798179625'}], 'has_more': False, 'result_num': 1, 'log_id': 1546142604240865587}\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import base64\n",
    "\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/realtime_search/same_hq/search\"\n",
    "\n",
    "f = open('images/pen.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = xiaoyang1_API\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "35b78e55",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'result': [{'score': 0.99732199081379,\n",
       "   'brief': '1',\n",
       "   'cont_sign': '2608927001,1798179625'}],\n",
       " 'has_more': False,\n",
       " 'result_num': 1,\n",
       " 'log_id': 1546142604240865587}"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "response.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bbbedf68",
   "metadata": {},
   "source": [
    "# 实时天气 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "id": "f653054d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import base64\n",
    "geo_url = \"https://devapi.qweather.com/v7/weather/now?\"\n",
    "\n",
    "payload = {\n",
    "    \"key\":\"100adc02d8ff4a5ca1020862b295bc2c\",\n",
    "    \"location\":\"101280103\",\n",
    "    \n",
    "}\n",
    "\n",
    "r = requests.get(url = geo_url,params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "b56e0fc4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "id": "0c5c2c36",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'code': '200',\n",
       " 'updateTime': '2022-07-10T22:52+08:00',\n",
       " 'fxLink': 'http://hfx.link/3hc1',\n",
       " 'now': {'obsTime': '2022-07-10T22:44+08:00',\n",
       "  'temp': '28',\n",
       "  'feelsLike': '29',\n",
       "  'icon': '150',\n",
       "  'text': '晴',\n",
       "  'wind360': '135',\n",
       "  'windDir': '东南风',\n",
       "  'windScale': '3',\n",
       "  'windSpeed': '17',\n",
       "  'humidity': '78',\n",
       "  'precip': '0.0',\n",
       "  'pressure': '990',\n",
       "  'vis': '30',\n",
       "  'cloud': '10',\n",
       "  'dew': '26'},\n",
       " 'refer': {'sources': ['QWeather', 'NMC', 'ECMWF'],\n",
       "  'license': ['no commercial use']}}"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a3c07765",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5210a836",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
